import sys
import os

from PyQt5 import QtWebEngineWidgets, QtGui
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QMovie, QPicture, QBitmap, QPixmap
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5.QtWidgets import *
from PyQt5.Qt import *


class WebEngineView(QtWebEngineWidgets.QWebEngineView):
    def createWindow(self, QWebEnginePage_WebWindowType):
        page = WebEngineView(self)
        page.urlChanged.connect(self.on_url_changed)
        return page

    def on_url_changed(self, url):
        print(url)
        self.setUrl(url)


class MainWindow(QMainWindow):
    closed=pyqtSignal()
    def __init__(self, ):
        super(QMainWindow, self).__init__()
        self.number = 0

        w = QWidget()
        self.setCentralWidget(w)
        w.setAttribute(Qt.WA_AcceptTouchEvents, True)
        self.topFiller = QWidget()
        self.topFiller.setMinimumSize(1950,2000)  #######设置滚动条的尺寸

        self.view = WebEngineView()
        self.view.setMinimumWidth(400)
        self.view.setMaximumWidth(1800)
        self.view.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls, True)
        self.view.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, True)
        self.view.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
        self.view.settings().setAttribute(QWebEngineSettings.PluginsEnabled,True)
        self.view.settings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True)
        #htmlpath = QUrl(r"K:/编程code/python/天音波影院/html漫画阅读器/漫画阅读器.html")
        htmlpath = QUrl(r"K:/编程code/python/pyqtPDF阅读器/out/index.html")
        self.view.load(htmlpath)
        self.view2=WebEngineView()
        self.view.page().setDevToolsPage(self.view2.page())
        self.view2.setMinimumHeight(500)
        def doScript():
            dirpath = QFileDialog.getExistingDirectory(directory="K:\美图")
            urllist = []

            def get_urllist(path):
                for root, dirs, files in os.walk(path, topdown=False):
                    for name in files:
                        url = root + "/" + name
                        urllist.append(url)
                return urllist

            get_urllist(dirpath)
            jsstr = f'''
            if(getimage){{
               
            }}
            getimage({urllist})          
            '''
            self.view.page().runJavaScript(jsstr,lambda x:print(f"{x}is done"))
        #self.view.page().scrollPositionChanged.connect(lambda x:print(x))
        self.btn1 = QPushButton("加载文件夹图片")
        self.btn1.clicked.connect(doScript)
        self.view.show()
        self.view2.show()
        self.scroll = QScrollArea()
        self.scroll.setWidget(self.topFiller)
        self.gridlayout = QVBoxLayout()
        self.topFiller.setLayout(self.gridlayout)
       # self.gridlayout.setSizeConstraint(QLayout.SetDefaultConstraint)
        self.gridlayout.addWidget(self.btn1)
        self.btn2=QPushButton("打开影片")
        self.btn2.clicked.connect(self.openVideo)
        self.btn3=QPushButton("关闭pdf")
        self.btn3.clicked.connect(lambda x:print(self.view.load(QUrl(r"K:/编程code/python/天音波影院/html漫画阅读器/漫画阅读器.html"))))
        self.gridlayout.addWidget(self.btn3)
        self.gridlayout.addWidget(self.btn2)
       # self.btn1.clicked.connect(self.load_img)
        # for i, k in enumerate(s):
        #     if i <= 50:
        #         self.MapButton = QLabel()
        #         p = QPixmap(k)
        #         self.MapButton.setPixmap(p)
        #         self.MapButton.setScaledContents(True)
        #         self.gridlayout.addWidget(self.MapButton)
        # 创建一个滚动条
        self.gridlayout.addWidget(self.view)
        self.gridlayout.addWidget(self.view2)
        self.vbox = QVBoxLayout()
        self.scroll.horizontalScrollBar().setVisible(False)
        self.vbox.addWidget(self.scroll)
        w.setLayout(self.vbox)
        self.initUi()
        self.statusBar().showMessage("底部信息栏")


    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        self.closed.emit()
        super().closeEvent(a0)

    def initUi(self):
        pass

    def load_img(self):
        for i in range(self.gridlayout.count()):
            if i == 0:
                continue
            print(self.gridlayout.count())
            self.gridlayout.itemAt(i).widget().deleteLater()
            print(f"删除{i}个")

    def openVideo(self):
        videopath=QFileDialog.getOpenFileName(directory=r"K:\美图")
        print(videopath)
#         jscode=f'''btn2.onclick=btn2clicked("dfkdjfkdjf");
# btn2.click()
# '''
        jscode=f'''getpdf({list(videopath)})'''
        self.view.page().runJavaScript(jscode,lambda x:print(f"{x}is done"))

if __name__ == "__main__":
    app = QApplication(sys.argv)

    mainwindow = MainWindow()
    mainwindow.resize(1980,1080)
    mainwindow.show()
    mainwindow.closed.connect(lambda x:mainwindow.view.destroy())
    mainwindow.closed.connect(lambda x:mainwindow.view2.destroy())
    sys.exit(app.exec_())
